package com.donger.business.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donger.auth.utils.SecurityUtils;
import com.donger.business.cms.mapper.CmsPostMapper;
import com.donger.business.cms.entity.CmsPost;
import com.donger.business.cms.service.CmsPostService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * cms文章(CmsPost)表服务实现类
 *
 * @author somliy
 * @since 2020-05-07 19:06:17
 */
@AllArgsConstructor
@Service("cmsPostService")
public class CmsPostServiceImpl extends ServiceImpl<CmsPostMapper, CmsPost> implements CmsPostService {

    private final CmsPostMapper cmsPostMapper;

    /**
     * 分页查询所有
     * @param page
     * @param cmsPost
     * @return
     */
    @Override
    public IPage postPage(Page<CmsPost> page, CmsPost cmsPost) {
        return cmsPostMapper.postPage(page, new QueryWrapper<CmsPostService>()
                .like(StrUtil.isNotBlank(cmsPost.getTitle()),"a.title",cmsPost.getTitle())
                .like(StrUtil.isNotBlank(cmsPost.getStatus()),"a.status",cmsPost.getStatus())
                .eq(cmsPost.getCategoryId()!=null,"a.category_id",cmsPost.getCategoryId())
        );
    }

    /**
     * 发布文章
     * @param cmsPost
     */
    @Override
    public CmsPost saveByPost(CmsPost cmsPost) {
        Long userId = SecurityUtils.getUser().getId();
        cmsPost.setUserId(userId);
        this.save(cmsPost);
        return cmsPost;
    }

    /**
     * 修改文章
     * @param cmsPost
     */
    @Override
    public void updateByPost(CmsPost cmsPost) {
        this.updateById(cmsPost);
    }
}